package com.hzy.admin.configs;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * mybatis plus MetaObjectHandler 配置 insert 和 update 填充
 * 
 * @author hzy
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    private final Logger log = LoggerFactory.getLogger(MybatisPlusMetaObjectHandler.class);

    /**
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ...." + LocalDateTime.now().toString());
        // 起始版本 3.3.0(推荐使用)
        // this.strictInsertFill(metaObject, "id", String.class,
        // UUID.randomUUID().toString());
        // 也可以使用(3.3.0 该方法有bug请升级到之后的版本如`3.3.1.8-SNAPSHOT`)
        // this.fillStrategy(metaObject, "createTime", LocalDateTime.now());
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }

    /**
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ...." + LocalDateTime.now().toString());
        // 起始版本 3.3.0(推荐使用)
        // this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class,
        // LocalDateTime.now());
        // 也可以使用(3.3.0 该方法有bug请升级到之后的版本如`3.3.1.8-SNAPSHOT`)
        // this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}
